//
//  ZSSegmentView.m
//  Fangxiaov
//
//  Created by safiri on 2017/3/11.
//  Copyright © 2017年 com.f. All rights reserved.
//

#import "ZSSegmentButtonView.h"


#define FirstButtonTag 10
#define LineViewTag 100

@interface ZSSegmentButtonView ()
@property (nonatomic ,strong, readwrite) NSArray *titleArray;
@property (nonatomic ,strong ,readwrite) ZSButtonTag *sameTag;
@property (nonatomic ,assign ,readwrite) NSInteger selectedTag;
@property (nonatomic ,strong) NSMutableArray *tagArray;
@property (nonatomic ,strong) UIView *lineViewContainer;

@property (nonatomic ,strong) UIButton *lastSelectedBtn;

@end

@implementation ZSSegmentButtonView

#pragma mark - set get

- (void)createOrRefreshSegViewWithTitleArray:(NSArray *)titleArray tag:(ZSButtonTag *)sameTag{
    self.titleArray = titleArray;
    _sameTag = sameTag;
    [self.tagArray removeAllObjects];
    for (NSString *title in self.titleArray) {
        [self.tagArray addObject:[self autoCreateTag:title]];
    }
    [self createUI];
}

- (NSMutableArray *)tagArray {
    if (!_tagArray) {
        _tagArray = [NSMutableArray array];
    }
    return _tagArray;
}

- (ZSButtonTag *)sameTag {
    if (!_sameTag) {
        _sameTag = [[ZSButtonTag alloc] init];
        UIColor *color = [UIColor colorWithRed:88/255.0 green:105/255.0 blue:129/255.0 alpha:1];
        _sameTag.textColor = color;
        _sameTag.slcTextColor = color;
        _sameTag.nrmBgImageColor = [UIColor whiteColor];
        _sameTag.font = [UIFont systemFontOfSize:14];
    }
    return _sameTag;
}

- (ZSButtonTag *)autoCreateTag:(NSString *)title {
    ZSButtonTag *tag = [[ZSButtonTag alloc] initWithSameTag:self.sameTag];
    tag.text = title;
    return tag;
}
- (UIView *)lineViewContainer {
    if (!_lineViewContainer) {
        _lineViewContainer = [[UIView alloc] init];
        _lineViewContainer.backgroundColor = [UIColor clearColor];
        _lineViewContainer.tag = LineViewTag;
        UIButton *button = [self viewWithTag:FirstButtonTag];
        [self addSubview:_lineViewContainer];
        [_lineViewContainer mas_makeConstraints:^(MASConstraintMaker *make) {
            self.lineViewLeftConstraint = make.left.equalTo(button.mas_left).offset(0);
            make.bottom.equalTo(self.mas_bottom).offset(0);
            make.width.mas_equalTo(button);
            make.height.mas_equalTo(2);
        }];
       
    }
    return _lineViewContainer;
}
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        [self.lineViewContainer addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            CGFloat padding = self.lineViewPaddingForLeftRight>0?:0;
            make.leading.equalTo(self.lineViewContainer.mas_leading).offset(padding);
            make.trailing.equalTo (self.lineViewContainer.mas_trailing).offset(-padding);
            make.top.equalTo(self.lineViewContainer.mas_top);
            make.bottom.equalTo(self.lineViewContainer.mas_bottom);
        }];
    }
    return _lineView;
}
- (void)setLineViewPaddingForLeftRight:(NSInteger)lineViewPaddingForLeftRight {
    _lineViewPaddingForLeftRight = lineViewPaddingForLeftRight;
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.lineViewContainer.mas_leading).offset(lineViewPaddingForLeftRight);
        make.trailing.equalTo (self.lineViewContainer.mas_trailing).offset(-lineViewPaddingForLeftRight);
        make.top.equalTo(self.lineViewContainer.mas_top);
        make.bottom.equalTo(self.lineViewContainer.mas_bottom);
    }];
}

- (void)setLineViewBackgroundColor:(UIColor *)lineViewBackgroundColor {
    _lineViewBackgroundColor = lineViewBackgroundColor;
    self.lineView.backgroundColor = lineViewBackgroundColor;
}

- (void)setLineViewHeight:(CGFloat)lineViewHeight {
    _lineViewHeight = lineViewHeight;
    [self.lineViewContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(lineViewHeight);
    }];
}
#pragma mark - UI
- (void)createUI {
    
    for (UIView *view in [self subviews]) {
        if (view.tag >= FirstButtonTag) {
            [view removeFromSuperview];
        }
    }
    for (NSInteger i=0; i<self.tagArray.count; i++) {
        ZSTagButton *button = [ZSTagButton buttonWithTag:self.tagArray[i]];
        [button addTarget:self action:@selector(topSegButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = FirstButtonTag+i;
        [self addSubview:button];
        
        [button mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (i==0) {
                make.leading.equalTo(self.mas_leading).offset(0);
            }else{
                
                make.left.equalTo(self.subviews[i-1].mas_right).offset(0.5);
            }
            if (i==self.tagArray.count-1) {
                make.trailing.equalTo(self.mas_trailing).offset(0);
            }
            make.top.equalTo(self.mas_top).offset(0);
            make.bottom.equalTo(self.mas_bottom).offset(-1);
            if (i-1>=0) {
                make.width.mas_equalTo(self.subviews[i-1]);
            }
            
        }];
    }
    self.selectedTag = 0;
    [self ceateLineView];
}

- (void)ceateLineView {
    self.lineViewContainer.hidden = NO;
    self.lineView.hidden = NO;
}
- (void)setSelectedTag:(NSInteger)selectedTag {
    _selectedTag = selectedTag;
    UIButton *selectedBtn = [self viewWithTag:selectedTag+FirstButtonTag];
    selectedBtn.selected = YES;
    if (self.lastSelectedBtn) {
        self.lastSelectedBtn.selected = NO;
    }
    self.lastSelectedBtn = selectedBtn;
}
- (void)selectSegmentButtonWithTag:(NSInteger)tag animated:(BOOL)animated {
    [self moveLineViewWithTag:tag animated:animated];
}
- (void)topSegButtonClick:(ZSTagButton *)button {
    NSInteger selectTag = button.tag - FirstButtonTag;
    if (self.selectedTag == selectTag) {
        return;
    }
    [self moveLineViewWithTag:selectTag animated:YES];
    if ([self.segmentButtonViewDelegate respondsToSelector:@selector(segmentButtonSelectTag:)]) {
        [self.segmentButtonViewDelegate segmentButtonSelectTag:button.tag - FirstButtonTag];
    }
}

- (void)moveLineViewWithTag:(NSInteger)selectTag animated:(BOOL)animated{
    self.selectedTag = selectTag;
    [self moveLineWithRatio:selectTag animated:animated];
}

- (void)moveLineWithRatio:(CGFloat)ratio animated:(BOOL)animated{
    UIView *lineView = [self viewWithTag:LineViewTag];
    UIButton *button = [self viewWithTag:FirstButtonTag];//只用第一个button
    [self.lineViewLeftConstraint uninstall];
    CGFloat duration = animated?0.5:0;
    [UIView animateWithDuration:duration animations:^{
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.lineViewLeftConstraint = make.left.equalTo(button.mas_left).offset(ratio * self.frame.size.width/self.titleArray.count);
        }];
        [lineView.superview layoutIfNeeded];
    }];
}
@end
